﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericTypes
{
    class Program
    {

        //交换算法
        static void Swap<T>(ref T a, ref T b)
        {
            T temp = a;
            a = b;
            b = temp;

        }
        static void Main(string[] args)
        {
            //泛型类
            //Stack<int> testStack = new Stack<int>();
            //int testvalue = 10;
            //testStack.push(testvalue);
            //int returnValue = testStack.Pop();
            //Console.WriteLine("pop value is ："+returnValue.ToString());

            //Stack<string> testStack2 = new Stack<string>();
            //testStack2.push("joey");
            //testStack2.push("lisa");
            //Console.WriteLine("pop value is ：" +testStack2.Pop());
            //Console.WriteLine("pop value is ：" + testStack2.Pop());

            //泛型方法
            //int a = 10;
            //int b = 5;
            //Console.WriteLine("交换之间的数据是a={0},b={1}",a,b);
            //Swap<int>(ref a,ref b);
            //Console.WriteLine("交换之后的数据是a={0},b={1}",a,b);
            //Console.Read();

            //typeof and Unbound Generic types
            //Type a1 = typeof(A<>);
            //Type a2 = typeof(A<,>);
            //Console.WriteLine("type of A<> is {0}",a1);
            //Console.WriteLine("type of A<,> is {0}",a2);
            //Console.Read();

            //Default type value
            //int[] array = new int[10];
            //Zap<int>(array);
            //for (int i = 0; i < array.Length; i++)
            //{
            //    Console.WriteLine("Default Value，第{0}个是数据是{1}",i,array[i]);
            //}


            //string[] arraystring = new string[10];
            //Zap<string>(arraystring);
            //for (int i = 0; i < arraystring.Length; i++)
            //{
            //    Console.WriteLine("Default Value，第{0}个是数据是{1}", i, arraystring[i]);
            //}

            //Console.Read();

            //泛型参数约束
            //Class2 TValue = new Class2();
            //int a = 100;
            //GenericClass<Class2, int> testGenericConstraints = new GenericClass<Class2, int>();
            //Console.WriteLine("TValue is " + testGenericConstraints.ReturnTName(TValue));
            //Console.WriteLine("UValue is " + testGenericConstraints.ReturnUName(a));
            //Console.Read();

            //ItemFactory<JoeyClass> testjoey = new ItemFactory<JoeyClass>();
            //Console.WriteLine(testjoey.GetNewItem());
            //Console.Read();


            Stack<Bear> bears = new Stack<Bear>();
            Stack<Animal> animal = bears;
            


        }
        //泛型方法
        static void Zap<T>(T[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = default(T);
            }
        }
    }

    class A<T>
    {

    }

    class A<T1, T2>
    {

    }


    class SomeClass { }
    interface Interface1 { };
    class Class2 : SomeClass,Interface1
    {
     
    }
    class GenericClass<T, U>
        where T : SomeClass, Interface1
        where U : new()
    {

        public string ReturnTName(T value)
        {
            return value.GetType().Name;
        }

        public string ReturnUName(U value)
        {
            return value.GetType().Name;
        }

    }

    //用来测试泛型参数约束new()
    class ItemFactory<T> where T : new()
    {
        public T GetNewItem()
        {
            return new T();
        }
    }

    class JoeyClass
    {
        public string className
        {
             get;
             set;
        }
        public JoeyClass(string a)
        {
            className = "JoeyClass";
        }
    }


    //协变Covariance

    class Animal { }
    class Bear : Animal { }
    class Camel : Animal { }

  

}
